package com.liuyi.netty.im.lychat.modular.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuyi.netty.im.commons.codes.data.event.Session;
import com.liuyi.netty.im.lychat.modular.caches.ChatSessionCache;
import com.liuyi.netty.im.lychat.modular.entity.ChatSession;
import com.liuyi.netty.im.lychat.modular.enums.SessionType;
import com.liuyi.netty.im.lychat.modular.mapper.ChatSessionMapper;
import com.liuyi.netty.im.lychat.modular.service.IChatSessionService;
import com.liuyi.netty.im.lychat.modular.service.IGroupMemberService;
import com.liuyi.netty.im.lychat.modular.service.IUserFriendService;
import com.liuyi.netty.im.server.util.ImHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 聊天会话表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-12-29
 */
@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession> implements IChatSessionService {

    @Resource
    private IUserFriendService userFriendService;
    @Resource
    private IGroupMemberService groupMemberService;
    @Resource
    private ChatSessionCache chatSessionCache;

    @Override
    public ChatSessionCache cache() {
        return chatSessionCache;
    }

    @Override
    public ChatSession createSession(String userId, String toId, SessionType sessionType) {
        String sessionId = ImHelp.sessionId(sessionType.sessionType(), toId);
        ChatSession chatSession = getOne(q().eq(ChatSession.USER_ID, userId).eq(ChatSession.SESSION_ID, sessionId).eq(ChatSession.SESSION_TYPE, sessionType));
        if (chatSession != null) {
            return chatSession;
        }
        switch (sessionType) {
            case GROUP:
                if (groupMemberService.cache().countByUserIdAndGroupId(userId, toId) <= 0) {
                    return null;
                }
                break;
            case C2C:
                if (userFriendService.cache().getByUserIdAndFriendUserId(userId, toId) == null) {
                    return null;
                }
                break;
        }
        chatSession = new ChatSession();
        chatSession.setSessionId(ImHelp.sessionId(sessionType.sessionType(), toId))
                .setSessionType(sessionType).setUserId(userId).setToId(toId);
        this.save(chatSession);
        return chatSession;
    }

    @Override
    public List<Session> wrapperSession(List<ChatSession> chatSessions) {
        List<Session> result = new ArrayList<>(chatSessions.size());
        chatSessions.forEach(chatSession -> result.add(wrapperSession(chatSession)));
        return result;
    }

    @Override
    public Session wrapperSession(ChatSession chatSession) {
        if (chatSession == null) {
            return null;
        }
        Session session = new Session();
        String userId = chatSession.getUserId();
        String sessionId = chatSession.getSessionId();
        SessionType sessionType = chatSession.getSessionType();
        String toId = ImHelp.sessionIdToId(sessionId);
        session.setUserId(userId);
        session.setSessionId(sessionId);
        session.setSessionType(sessionType.sessionType());
        session.setUnreadCount(chatSessionCache.getUnreadCount(userId, sessionId).intValue());
        session.setLastMessage(chatSessionCache.getLastMessage(userId, sessionId));
        switch (sessionType) {
            case C2C:
                session.setUserProfile(userFriendService.cache().getByUserIdAndFriendUserId(userId, toId));
                break;
            case GROUP:
                session.setGroupProfile(groupMemberService.cache().getByUserIdAndGroupId(userId, toId));
                break;
        }
        return session;
    }
}
